#include <iostream>
#include <string>
#include <jsoncpp/json/json.h>

int main(){
    // 1.创建JSON对象
    Json::Value root;
    root["name"] = "张三";
    root["age"] = 25;
    root["scores"] = Json::Value(Json::arrayValue);
    root["scores"].append(85);
    root["scores"].append(92);
    root["scores"].append(78);

    // 2.序列化方法一：使用 FastWriter(紧凑格式)
    Json::FastWriter writer1;
    std::string json_str1 = writer1.write(root);
    std::cout << "FastWriter 格式输出: " << json_str1 << std::endl;

    // 3.序列化方法二：使用 StyledWriter(美化格式)
    Json::StyledWriter writer2;
    std::string json_str2 = writer2.write(root);
    std::cout << "StyledWriter 格式输出: " << json_str2 << std::endl;


    // 4.反序列化
    Json::Value v;
    Json::Reader reader;
    bool r = reader.parse(json_str1, v);
    if(r){
        // 读取数据
        std::string name = v["name"].asString();
        int age = v["age"].asInt();
        std::cout << name << " " << age << " ";

        // 读取数组
        for (int i = 0; i<v["scores"].size(); i++){
            int scores = v["scores"][i].asInt();
            std::cout << scores << " ";
        }
        std::cout << std::endl;
    }

    return 0;
}